<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//消息管理
class Message extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('System/MessageModel','CurModel');
		
		$this->load->model('User/UserModel','UserModel');
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		
		
		$user_id = $this->user_id;
		
		$filter = array('title','date','type','user_name');
		$arr = array(
			'filter'=>$filter
		);
		$appends = array(
			'user_id'=>$user_id
		);
		$result = parent::_get_list($arr,$appends);
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	
	
	
	function info_out($info){
		$info['addtime'] = date('Y-m-d H:i:s',$info['addtime']);
		
		$ls_user = $this->UserModel->get_user($info['user_id']);
		if($ls_user){
			$info['user_name'] = $ls_user['nickname'];
		}
		else{
			$info['user_name'] = '';
		}
		
		
		$ls_user = $this->UserModel->get_user($info['target_id']);
		if($ls_user){
			$info['target_name'] = $ls_user['nickname'];
		}
		else{
			$info['target_name'] = '';
		}
		
		
		
		return $info;
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function get_info(){
		$user_id = $this->user_id;
		
		$result = parent::_get_info();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function save(){
		$user_id = $this->user_id;
		
		if(!$this->check_priv('company_update|company_add')){
			//$this->err_out(LANG_NOPRIV);
		}
		
		$filter = array(
			'target_id'=>'请选择发送目标',
			'title'=>'请输入标题',
			'content'=>'请输入内容'
		);
		$post = array('target_id','title','content');
		$farr = array(
			'post'=>$post,
			'filter'=>$filter
		);
		$time = time();
		
		$arr = array(
			'user_id'=>$user_id,
			'addtime'=>$time,
			'type'=>1
		);
		
		$result = parent::_save($farr,$arr);
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->add_log("发送站内信息【{$result['id']}】",'bumen');
		$this->data_out($result);
	}
	
	
	
	//start
	/**
	* @name 删除
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del(){
		
		if(!$this->check_priv('company_del')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$result = parent::_del();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		
		$this->add_log("删除主体公司【{$result}】",'bumen');
		$this->data_out($result);
		
	}
	
	//start
	/**
	* @name 批量操作
	* @param ids ture int_array ID组合
	* @param act ture int 操作类型enable，disconnected，delete
	* @return 成功返回1
	*/
	//end
	function batch_act(){
		$ids = $this->input->post('ids',null,array());
		$act = $this->input->post('act',null,'');
		
		$act_arr = array('delete');
		if(!in_array($act,$act_arr)){
			$this->err_out('错误的错误');
		}
		if(!is_array($ids)){
			$ids = explode(',',$ids);
		}
		if(count($ids)<=0){
			$this->err_out('请选择成员');
		}
		$ls_ids = array();
		foreach($ids as $id){
			if(!empty($id)){
				$ls_ids[] = $id;
			}
		}
		
		if($act=='delete'){
			$this->CurModel->del("id in(".implode(',',$ls_ids).")");
		}
		
		$this->data_out(1);
	}
	//start
	/**
	* @name 设置是否启用
	* @param id ture int ID
	* @param status ture int 状态
	* @return 成功返回1
	*/
	//end
	function set_enable(){
		$id = $this->input->post('id',null,0);
		$status = $this->input->post('status',null,0);
		$info = $this->CurModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->CurModel->edit(array('is_enable'=>$status),"id ='{$id}'");
		
		$this->data_out(1);
	}
	
	//start
	/**
	* @name 获取最新的
	* @return 成功返回1
	*/
	//end
	function get_new(){
		$user_id = $this->user_id;
		$this->load->dmodel('msg_template','msg_template');
		
		$res = $this->msg_template->getAll();
		$info = array(
			'count'=>0,
			'list'=>array()
		);
		$arr = array();
		$count = 0;
		
		$this->load->model('System/MessageModel','MessageModel');
		
		foreach($res as $val){
			$ls_num = $this->MessageModel->getCount(array('target_id'=>$user_id,'is_view'=>0,'msg_type'=>$val['id']));
			
			$msg_num = $ls_num?$ls_num:0;
			
			$arr[] = array(
				'name'=>$val['msg_type'],
				'msg_num'=>$msg_num
			);
			$count += $msg_num;
		}
		$info['list'] = $arr;
		$info['count'] = $count;
		
		$this->data_out($info);
	}
	
	//start
	/**
	* @name 设置查看状态
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function view(){
		$id = $this->input->post('id',null,0);
		
		$info = $this->CurModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->CurModel->edit(array('is_view'=>1),"id ='{$id}'");
		
		$this->data_out(1);
	}
}
